package csm.web.core.sitemap;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServlet;

import org.jdom.Element;

import csm.common.StringUtil;
import csm.web.core.VelocityServletBase;

/**
 * SimteMap의 세부 엘리먼트인 pathInfo의 모델 클래스
 * @author seungmun
 *
 */
public final class PathInfo
{
	private String path;
	private String handler;
	private String action;
	private String target;
	private String template;
	private String templateFile;
	private String viewPage;
	private String checkLogin;
	private String checkAdmin;
	private String checkUser;

	private Class<?> clazz;

	public void buildHandler() throws Exception
	{
		if (StringUtil.isNullOrEmpty(handler) && !isRedirectAction() && !isForwardAction())
			throw new Exception("Invalid Path Infomation from Sitemap. see [" + this.path + "] settings");

		if (!isRedirectAction() && !isForwardAction()) {
			try {
				clazz = Class.forName(getHandler());
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public Object getInstance(ServletConfig servletConfig) throws Exception {
		Object obj = clazz.newInstance();
		if (obj == null)
			return obj;
		if (obj instanceof VelocityServletBase) {
			VelocityServletBase servlet = (VelocityServletBase)obj;
			servlet.setPathInfo(this);
			servlet.init(servletConfig);
		} else if (obj instanceof HttpServlet) {
			HttpServlet servlet = (HttpServlet)obj;
			servlet.init(servletConfig);
		}
		return obj;
	}

	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getHandler() {
		return handler;
	}
	public void setHandler(String handler) {
		this.handler = handler;
	}
	public String getAction() {
		return action;
	}
	public void setAction(String action) {
		this.action = action;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getTemplate() {
		return template;
	}
	public void setTemplate(String template) {
		this.template = template;
	}
	public String getTemplateFile() {
		return templateFile;
	}
	/**
	 * 이 객체가 사용할 템플릿 파일의 경로<br>
	 * {@link SiteMap}객체를 통해 자동으로 생성된다.
	 * @param templateFile
	 */
	protected void setTemplateFile(String templateFile) {
		this.templateFile = templateFile;
	}
	public String getViewPage() {
		return viewPage;
	}
	public void setViewPage(String viewPage) {
		this.viewPage = viewPage;
	}
	public String getCheckLogin() {
		return checkLogin;
	}
	public void setCheckLogin(String checkLogin) {
		this.checkLogin = checkLogin;
	}
	public String getCheckAdmin() {
		return checkAdmin;
	}
	public void setCheckAdmin(String checkAdmin) {
		this.checkAdmin = checkAdmin;
	}
	public String getCheckUser() {
		return checkUser;
	}
	public void setCheckUser(String checkUser) {
		this.checkUser = checkUser;
	}

	private List<String> checkUserItems;
	private void buildCheckUserItems()
	{
		if( checkUserItems == null)
			this.checkUserItems = new ArrayList<String>();

		this.checkUserItems.clear();
		if( StringUtil.isNullOrEmpty(this.checkUser)) return;

		String[] items = StringUtil.split(this.checkUser, "|");
		for(String item : items){
			if( StringUtil.isNullOrEmpty(item)) continue;
			this.checkUserItems.add(item);
		}
	}

	public List<String> getCheckUserItems() {
		if (checkUserItems == null)
			buildCheckUserItems();
		return this.checkUserItems;
	}

	public boolean isRedirectAction() {
		if (StringUtil.isNullOrEmpty(this.action))
			return false;
		if (this.action.equalsIgnoreCase("redirect"))
			return true;
		return false;
	}

	public boolean isForwardAction() {
		if (StringUtil.isNullOrEmpty(this.action))
			return false;
		if (this.action.equalsIgnoreCase("forward"))
			return true;
		return false;
	}

	public boolean isLoginRequired() {
		if (StringUtil.isNullOrEmpty(this.checkLogin))
			return false;
		if (this.checkLogin.equalsIgnoreCase("Y"))
			return true;
		return false;
	}

	public boolean isAdminRequired() {
		if (StringUtil.isNullOrEmpty(this.checkAdmin))
			return false;
		if (this.checkAdmin.equalsIgnoreCase("Y"))
			return true;
		return false;
	}

	public boolean isUserDetailCheckRequired() {
		if (checkUserItems == null)
			buildCheckUserItems();

		if (this.checkUserItems.size() > 0)
			return true;
		return false;
	}

	public void parse(Element element){
		this.path= element.getAttributeValue("path");
		this.handler= element.getAttributeValue("handler");
		this.action= element.getAttributeValue("action");
		this.target= element.getAttributeValue("target");
		this.template= element.getAttributeValue("template");
		//this.templateFile= element.getAttributeValue("path");
		this.viewPage= element.getAttributeValue("viewPage");
		this.checkLogin= element.getAttributeValue("checkLogin");
		this.checkAdmin= element.getAttributeValue("checkAdmin");
		this.checkUser= element.getAttributeValue("checkUser");
		buildCheckUserItems();
		try {
			buildHandler();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
